package com.dinetech.dao;

import java.io.IOException;
import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.dinetech.entidades.Evento;
import com.dinetech.entidades.Eventos;
import com.dinetech.entidades.Personal;
import com.dinetech.entidades.Session;
import com.dinetech.entidades.SpinnerVarios;
import com.dinetech.entidades.Ubicacion;

public class WSEvento {

	final String NAMESPACE = "http://www.CuyTechService.pe/WSservice2005/";//"http://192.168.1.10/WebService/";
	final String URL= "http://www.cuytechservice.pe/WsCuyTech2005/SoapDataServices.asmx";//"http://192.168.1.10/WSservice2005/SoapDataServices.asmx";
	
	public ArrayList<Evento> listarEventos (String nivelEvento){
		
		ArrayList<Evento> listaArray= new ArrayList<Evento>();
	    		
		final String METHOD_NAME = "ListarNombreDeEventos";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;//"http://www.CuyTechService.pe/WSservice2005/ListarNombreDeEventos";
		
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("IdMTablaNivel", 11);
		request.addProperty("IdMTablaTipoEvento", 12);
		request.addProperty("CodNivelEvento", nivelEvento);
		request.addProperty("EstadoEvento", "A");
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		
			HttpTransportSE transporte = new HttpTransportSE(URL);
			
			
			try{
				transporte.call(SOAP_ACTION, envelope);				
				SoapObject respSoap=(SoapObject)envelope.getResponse();
				
		
				for(int i=0; i < respSoap.getPropertyCount(); i++){
						
					SoapObject ic =(SoapObject)respSoap.getProperty(i);
					listaArray.add(new Evento(Integer.parseInt(ic.getPropertyAsString("Valor")), ic.getPropertyAsString("Descripcion").toString(), ic.getPropertyAsString("CodNivel")));				
					
				}
			
			}catch(Exception e){
				
			}			
		
		return listaArray ;
	}
	
	public ArrayList<Ubicacion> ubicacionPorEvento (String CodTipoEvento, int IdGalpon, int Cod1, int Cod2, int TipoBusq, int IdUbic, int IdSuc){
		
		ArrayList<Ubicacion> listaArray= new ArrayList<Ubicacion>();
	    
		final String METHOD_NAME = "UbicacionesPorTipoEvento";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("CodTipoEvento", CodTipoEvento);
		request.addProperty("IdGapon", IdGalpon);
		request.addProperty("Cod1", Cod1);
		request.addProperty("Cod2", Cod2);
		request.addProperty("TipoBusqueda", TipoBusq);
		request.addProperty("IdUbicacion", IdUbic);
		request.addProperty("IdSucursal", IdSuc);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		
			HttpTransportSE transporte = new HttpTransportSE(URL);
			
			
			try{
				transporte.call(SOAP_ACTION, envelope);				
				SoapObject respSoap=(SoapObject)envelope.getResponse();
				
				if(respSoap.getPropertyCount()>0){
					for(int i=0; i < respSoap.getPropertyCount(); i++){
						
						SoapObject ic =(SoapObject)respSoap.getProperty(i);
						listaArray.add(new Ubicacion(Integer.parseInt(ic.getPropertyAsString("IdUbicacion")),ic.getPropertyAsString("IdClienteUbicacion")));				
					}
				}else{
					listaArray.add(new Ubicacion(0, "No hay pozas / jaulas para este evento"));
				}
			
			}catch(Exception e){
				
			}			
		
		return listaArray ;
	}
	
	public ArrayList<Ubicacion> ubicacionPorEventoSession (int IdSucursal, int IdGalpon, int Cod1, int Cod2){
		
		ArrayList<Ubicacion> listaArray= new ArrayList<Ubicacion>();	    
		final String METHOD_NAME = "UbicacionesPorTipoEvento";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("IdSucursal", IdSucursal);
		request.addProperty("IdGapon", IdGalpon);
		request.addProperty("Cod1", Cod1);
		request.addProperty("Cod2", Cod2);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		
			HttpTransportSE transporte = new HttpTransportSE(URL);
			
			
			try{
				transporte.call(SOAP_ACTION, envelope);				
				SoapObject respSoap=(SoapObject)envelope.getResponse();
				
				for(int i=0; i < respSoap.getPropertyCount(); i++){
					
					SoapObject ic =(SoapObject)respSoap.getProperty(i);
					listaArray.add(new Ubicacion(Integer.parseInt(ic.getPropertyAsString("IdUbicacion")),ic.getPropertyAsString("IdClienteUbicacion")));				
				}
			
			}catch(Exception e){
				
			}			
		
		return listaArray ;
	}

	public ArrayList<SpinnerVarios> listarTipoTratamiento (int IdGranja){
		
		ArrayList<SpinnerVarios> listaArray= new ArrayList<SpinnerVarios>();	    		
		final String METHOD_NAME = "ListarTipoTratamiento";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("IdGranja", IdGranja);	
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		
			HttpTransportSE transporte = new HttpTransportSE(URL);
			
			
			try{
				transporte.call(SOAP_ACTION, envelope);				
				SoapObject respSoap=(SoapObject)envelope.getResponse();
				
				for(int i=0; i < respSoap.getPropertyCount(); i++){
					
					SoapObject ic =(SoapObject)respSoap.getProperty(i);
					listaArray.add(new SpinnerVarios(Integer.parseInt(ic.getPropertyAsString("Valor")),ic.getPropertyAsString("Descripcion")));				
				}
			
			}catch(Exception e){
				
			}			
		
		return listaArray ;
	}

	public void llenarListaDeUbicaciones(int idGalpon){
		
		Personal p = Personal.getInstance();
		Session s = Session.getInstance();
		int idSuc = p.getIdSucursal();

		s.setLISTA_UBICACION_EMPADRE_DYS_MORT_PARTO(ubicacionPorEventoSession(idSuc, idGalpon, 2, 3));
		s.setLISTA_UBICACION_RECRIA(ubicacionPorEventoSession(idSuc, idGalpon, 1, 1));
		s.setLISTA_UBICACION_TI_TRATMTO_SELEC_IE_CENSO(ubicacionPorEventoSession(idSuc, idGalpon, 20, 20));
		
	}
	
	public String grabarEvento (String CodNivelString, String CodTipoEString, String CodSubTipoEString, String CodLineaGeneticaString, String FechaEventoString,
            String IdOrigenString, String IdDestinoString, int CantMInt, int CantHInt, int CantGInt, String PesoMString, String PesoHString,
            String CodRutaString, String CodGeneroString, int IdReproductorInt, String ComentarioString, int IdPersonalInt,
            String FechaCreacionString, int IdPersonalCreacionInt, int IdBioseguridadInt, String CodTipoSeleccionString,
            int IdSucursalInt, int IdGalponInt, int CantABInt, int CantNMInt){
		
		String Msg="";	    		
		final String METHOD_NAME = "Eventos_insert";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		if(ComentarioString.length()==0){
			ComentarioString =" ";
		}
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("CodNivel", CodNivelString);
		request.addProperty("CodTipoE", CodTipoEString);
		request.addProperty("CodSubTipoE", CodSubTipoEString);
		request.addProperty("CodLineaGenetica", CodLineaGeneticaString);
		request.addProperty("FechaEvento", FechaEventoString);
		request.addProperty("IdOrigen", IdOrigenString);
		request.addProperty("IdDestino", IdDestinoString);
		request.addProperty("CantM", CantMInt);
		request.addProperty("CantH", CantHInt);
		request.addProperty("CantG", CantGInt);
		request.addProperty("PesoM", PesoMString);
		request.addProperty("PesoH", PesoHString);
		request.addProperty("CodRuta", CodRutaString);
		request.addProperty("CodGenero", CodGeneroString);
		request.addProperty("IdReproductor", IdReproductorInt);
		request.addProperty("Comentario", ComentarioString);		
		request.addProperty("IdPersonal", IdPersonalInt);
		request.addProperty("FechaCreacion", FechaCreacionString);
		request.addProperty("IdPersonalCreacion", IdPersonalCreacionInt);
		request.addProperty("IdBioseguridad", IdBioseguridadInt);
		request.addProperty("CodTipoSeleccion", CodTipoSeleccionString);
		request.addProperty("IdSucursal", IdSucursalInt);
		request.addProperty("IdGalpon", IdGalponInt);
		request.addProperty("CantAB", CantABInt);
		request.addProperty("CantNM", CantNMInt);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
			HttpTransportSE transporte = new HttpTransportSE(URL);
			
			try{
				transporte.call(SOAP_ACTION, envelope);								
				SoapPrimitive resultado_xml = (SoapPrimitive)envelope.getResponse();
					Msg = resultado_xml.toString();				
				
			
			}catch(Exception e){
				Msg = e.getMessage();
			}			
		
		return Msg ;
	}

	public ArrayList<Eventos> WSListarEventosRegistrados (int IdEvento){
		
		final String METHOD_NAME = "Eventos_detail";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		ArrayList<Eventos> lisEvento = new ArrayList<Eventos>();
		
     	SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("IdEvento", IdEvento);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		//envelope.bodyIn = request;
		envelope.dotNet = true;
		//envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		
			
            HttpTransportSE transporte = new HttpTransportSE(URL);
            //transporte.debug = true;
			try{
				transporte.call(SOAP_ACTION, envelope);				
				SoapObject respSoap=(SoapObject)envelope.getResponse();
				
				if(respSoap.getPropertyCount()>0){
					for(int i=0; i < respSoap.getPropertyCount(); i++){
				
					
					SoapObject ic =(SoapObject)respSoap.getProperty(i);
					
					lisEvento.add(new Eventos(ic.getPropertyAsString("CodTipoEvento").toString(),Integer.parseInt(ic.getPropertyAsString("IdEvento").toString()), ic.getPropertyAsString("NombreTipoEvento").toString(),
							Integer.parseInt(ic.getPropertyAsString("CantMachos").toString()),Integer.parseInt(ic.getPropertyAsString("CantHembras").toString()),Integer.parseInt(ic.getPropertyAsString("CantGazapos").toString()),
							ic.getPropertyAsString("Comentario").toString(),Integer.parseInt(ic.getPropertyAsString("CodLineaGenetica")),Integer.parseInt(ic.getPropertyAsString("CodSubTipoEvento")),
							ic.getPropertyAsString("IdClientUbicacionOrigen"),Integer.parseInt(ic.getPropertyAsString("CodGenero")),Integer.parseInt(ic.getPropertyAsString("CodRuta")),Integer.parseInt(ic.getPropertyAsString("IdBioSeguridad")),
							Integer.parseInt(ic.getPropertyAsString("CodTipoSeleccion")),Integer.parseInt(ic.getPropertyAsString("IdUbicacionOrigen"))));
					}
				}
				else
				{
					lisEvento.add(new Eventos("0",Integer.parseInt("0"), "No hay eventos registrados",Integer.parseInt("0"),Integer.parseInt("0"),Integer.parseInt("0"),"",Integer.parseInt("0"),
							Integer.parseInt("0"),"",Integer.parseInt("0"),Integer.parseInt("0"),Integer.parseInt("0"),Integer.parseInt("0"),Integer.parseInt("0")));
				}
				
				} catch (IOException e) {
				    e.printStackTrace();
				} catch (XmlPullParserException e) {
				    e.printStackTrace();
				              	
				} 
						return lisEvento;
						
	}

	public ArrayList<Eventos> WSListarEventos (int IdGalpon){
				
		final String METHOD_NAME = "Listar_Eventos";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		ArrayList<Eventos> lisEvento = new ArrayList<Eventos>();
		
		Personal objPer = Personal.getInstance();
		int IdPersonal = objPer.getIdPersonal();
		String FechaCreacion = objPer.obtenerFechaActual();
		
     	SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("IdGalpon", IdGalpon);
		request.addProperty("IdPersonal", IdPersonal);
		request.addProperty("fecha", FechaCreacion);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		//envelope.bodyIn = request;
		envelope.dotNet = true;
		//envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		
			
            HttpTransportSE transporte = new HttpTransportSE(URL);
            //transporte.debug = true;
			try{
				transporte.call(SOAP_ACTION, envelope);				
				SoapObject respSoap=(SoapObject)envelope.getResponse();
				
				if(respSoap.getPropertyCount()>0){
					for(int i=0; i < respSoap.getPropertyCount(); i++){
				
					
					SoapObject ic =(SoapObject)respSoap.getProperty(i);
					
					lisEvento.add(new Eventos(ic.getPropertyAsString("Valor").toString(),Integer.parseInt(ic.getPropertyAsString("IdEvento").toString()), ic.getPropertyAsString("Descripcion").toString(),ic.getPropertyAsString("IdClientUbicacionOrigen").toString()));
					}
				}
				else
				{
				lisEvento.add(new Eventos("0",Integer.parseInt("0"), "No hay eventos registrados",""));
				}
				
				} catch (IOException e) {
				    e.printStackTrace();
				} catch (XmlPullParserException e) {
				    e.printStackTrace();
				              	
				} 
						return lisEvento;
		
	}

	public ArrayList<Eventos> cantidadDePoblacionPorUbicacion (int IdUbicacion, int IdGalpon){
		
		ArrayList<Eventos> listaArray= new ArrayList<Eventos>();	    	
		final String METHOD_NAME = "CantidadPoblacion_x_Ubicacion";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("IdUbicacion", IdUbicacion);
		request.addProperty("IdGalpon", IdGalpon);		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		
			HttpTransportSE transporte = new HttpTransportSE(URL);
			
			
			try{
				transporte.call(SOAP_ACTION, envelope);				
				SoapObject respSoap=(SoapObject)envelope.getResponse();
				
				for(int i=0; i < respSoap.getPropertyCount(); i++){
					
					SoapObject ic =(SoapObject)respSoap.getProperty(i);
					listaArray.add(new Eventos(Integer.parseInt(ic.getPropertyAsString("CantMachos")),Integer.parseInt(ic.getPropertyAsString("CantHembras")), Integer.parseInt(ic.getPropertyAsString("CantGazapos")) ));				
				}
			
			}catch(Exception e){
				
			}			
		
		return listaArray ;
	}
		
	public ArrayList<Ubicacion> listarReproductores (int Idgranja, int IdSucursal, int IdGalpon, String TipoEvento){
		
		ArrayList<Ubicacion> listaArray= new ArrayList<Ubicacion>();
	    		
		final String METHOD_NAME = "Listar_Reproductor";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("IdGranja", Idgranja);
		request.addProperty("IdSucursal", IdSucursal);
		request.addProperty("IdGalpon", IdGalpon);
		request.addProperty("TipoEvento", TipoEvento);
	
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		
			HttpTransportSE transporte = new HttpTransportSE(URL);
			
			
			try{
				transporte.call(SOAP_ACTION, envelope);				
				SoapObject respSoap=(SoapObject)envelope.getResponse();
				
				if(respSoap.getPropertyCount()> 0){
					for(int i=0; i < respSoap.getPropertyCount(); i++){
						
						SoapObject ic =(SoapObject)respSoap.getProperty(i);
						listaArray.add(new Ubicacion(Integer.parseInt(ic.getPropertyAsString("IdUbicacionOrigen")),ic.getPropertyAsString("Valor"), ic.getPropertyAsString("CodGenero"), Integer.parseInt(ic.getPropertyAsString("IdReproductor"))));				
					}
				}else{
					listaArray.add(new Ubicacion(0, "No hay reproductores "));
				}
			
			}catch(Exception e){
				
			}			
		
		return listaArray ;
	}

	public String ActualizarEvento (int IdEvento,String CodNivel, String CodTipoE, String CodSubTipoE, String CodLineaGenetica, String FechaEvento,
            String IdOrigen, String IdDestino, int CantM, int CantH, int CantG, String PesoM, String PesoH,
            String CodRuta, String CodGenero, int IdReproductor, String Comentario, int IdPersonal,
            String FechaActualizacion, int IdPersonalActualizacion, int IdBioseguridad, String CodTipoSeleccion){
		
		String Msg="";
	    		
		final String METHOD_NAME = "Eventos_update";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	
		request.addProperty("IdEvento", IdEvento);
		request.addProperty("CodNivel", CodNivel);
		request.addProperty("CodTipoE", CodTipoE);
		request.addProperty("CodSubTipoE", CodSubTipoE);
		request.addProperty("CodLineaGenetica", CodLineaGenetica);
		request.addProperty("FechaEvento", FechaEvento);
		request.addProperty("IdOrigen", IdOrigen);
		request.addProperty("IdDestino", IdDestino);
		request.addProperty("CantM", CantM);
		request.addProperty("CantH", CantH);
		request.addProperty("CantG", CantG);
		request.addProperty("PesoM", PesoM);
		request.addProperty("PesoH", PesoH);
		request.addProperty("CodRuta", CodRuta);
		request.addProperty("CodGenero", CodGenero);
		request.addProperty("IdReproductor", IdReproductor);
		request.addProperty("Comentario", Comentario);		
		request.addProperty("IdPersonal", IdPersonal);
		request.addProperty("FechaActualizacion", FechaActualizacion);
		request.addProperty("IdPersonalActualizacion", IdPersonalActualizacion);
		request.addProperty("IdBioseguridad", IdBioseguridad);
		request.addProperty("CodTipoSeleccion", CodTipoSeleccion);

		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		
			HttpTransportSE transporte = new HttpTransportSE(URL);
			
			
			try{
			//	transporte.call(SOAP_ACTION, envelope);				
			//	SoapObject respSoap=(SoapObject)envelope.getResponse();
				transporte.call(SOAP_ACTION, envelope);	
				SoapPrimitive resultado_xml = (SoapPrimitive)envelope.getResponse();
				Msg= resultado_xml.toString();
				//for(int i=0; i < respSoap.getPropertyCount(); i++){
					
				//	SoapObject ic =(SoapObject)respSoap.getProperty(i);
				//	Msg = ic.getPropertyAsString("Msg");				
				//}
			
			}catch(Exception e){
				Msg = e.getMessage();
			}			
		
		return Msg ;
	}

	public String bajaDeUbicacion (int TipoBaja, int IdUbicInicio, int IdUbicFin,  int IdGalpon, int IdSuc, String Obs){
			    
		final String METHOD_NAME = "Baja_de_Ubicaciones";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		String Msg="";
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("TipoBaja", TipoBaja);
		request.addProperty("IdUbicInicio", IdUbicInicio);
		request.addProperty("IdUbicFin", IdUbicFin);
		request.addProperty("IdGalpon", IdGalpon);
		request.addProperty("IdSucursal", IdSuc);
		request.addProperty("Observaciones", Obs);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		
			HttpTransportSE transporte = new HttpTransportSE(URL);
			
			
			try{
				transporte.call(SOAP_ACTION, envelope);				
				SoapPrimitive resultado_xml = (SoapPrimitive)envelope.getResponse();
				Msg= resultado_xml.toString();

			}catch(Exception e){
				
			}			
		
		return Msg ;
	}

}
